import Vue from 'vue'
import App from './App'
import config from './config.js'
import md5 from 'js-md5'
import utils from './common/untils.js'

Vue.config.productionTip = false


App.mpType = 'app'

Vue.prototype.config = config

const app = new Vue({
	...App
})
app.$mount()
Vue.prototype.api_url = config.api_url;
//get请求
Vue.prototype._get = function(path, data, success, fail, complete) {
	data = data || {};
	let api_url = config.api_url;
	console.log(api_url);
	let user_token = uni.getStorageSync('user_token') || '';
	if (user_token == '') {
		uni.navigateTo({
			url: '/pages/login/login',
		});
	}
	uni.request({
		url: api_url+'/api/' + path,
		data: data,
		header: {
			'User-Token': user_token
		},
		dataType: 'json',
		method: 'GET',
		success: (res) => {
			let data = res.data;
			if (data.code == -880) {
				uni.removeStorageSync('user_token');
				uni.navigateTo({
					url: '/pages/login/login',
				});
			} else if (data.code == 1) {
				success && success(data);
			} else {
				let msg = data.msg;
				console.log(data);
			}
		},
		fail: (res) => {
			fail && fail(res);
		},
		complete: (res) => {
			uni.hideLoading();
			complete && complete(res);
		},
	});
};
/*
 post请求
*/
Vue.prototype._post = function(path, data, success, fail, complete) {
	data = data || {};
	let api_url = config.api_url;
	let user_token = uni.getStorageSync('user_token') || '';
	if (user_token == '') {
		uni.navigateTo({
			url: '/pages/login/login',
		});
	}
	uni.request({
		url:api_url+'/api/' + path,
		data: data,
		header: {
			'User-Token': user_token
		},
		dataType: 'json',
		method: 'POST',
		success: (res) => {
			let data = res.data;
			if (data.code == -880) {
				uni.removeStorageSync('user_token');
				uni.navigateTo({
					url: '/pages/login/login',
				});
			} else if (data.code == 1) {
				success && success(data);
			} else {
				let msg = data.msg;
				console.log(data);
				self.showError(msg);
			}
		},
		fail: (res) => {
			fail && fail(res);
		},
		complete: (res) => {
			uni.hideLoading();
			complete && complete(res);
		},
	});
};

Vue.prototype.setAccessToken = function() {
	let config = this.config;
	let rand_str = utils.randomString(5);
	let timestamp = (new Date()).getTime();
	timestamp = Math.round(timestamp / 1000);
	let signature_str = 'app_id=' + config.app_id + '&app_secret=' + config.app_secret + '&device_id=web&rand_str=' +
		rand_str + '&timestamp=' + timestamp;
	let signature = md5(signature_str);
	uni.request({
		url: this.api_url + '/api/BuildToken/getAccessToken',
		data: {
			app_id: config.app_id,
			device_id: 'web',
			rand_str: rand_str,
			timestamp: timestamp,
			signature: signature
		},
		header: {
			'access-token': config.app_secret
		},
		success: (res) => {
			console.log(res);
			let result = res.data;
			if (result.code == 1) {
				let data = result.data;
				uni.setStorageSync('access_token', data.access_token);
			}
		},
		fail: (res) => {
			console.log(res);
		}
	})
}

Vue.prototype.showError = function(msg, callback) {
	uni.showModal({
		title: '注意',
		content: msg,
		showCancel: false,
		success: function(res) {
			callback && callback(res);
		}
	});
};

/**
 * 显示成功提示框
 */
Vue.prototype.showSuccess = function(msg, callback) {
	uni.showModal({
		title: '提示',
		content: msg,
		showCancel: false,
		success: function(res) {
			callback && callback(res);
		}
	});
};
